package Yangtz.Cs.DAO;


import Yangtz.Cs.Entity.BorrowBean;
import Yangtz.Cs.Utils.JDBCUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class BorrowDao {
    //添加用户
    public boolean insert(BorrowBean borrowBean) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String sql = null;
        try {
            // 获得数据的连接
            conn = new JDBCUtil().GetConnection();
            // 获得Statement对象
            stmt = conn.createStatement();
            // 发送SQL语句
            sql = "INSERT INTO TB_Borrow " +
                    "VALUES('"
                    + borrowBean.getRdID()
                    + "','"
                    + borrowBean.getBkID()
                    + "','"
                    + borrowBean.getLdDateOut()
                    + "','"
                    + borrowBean.getLdDateRetPlan()
                    + "','"
                    + borrowBean.getLdDateRetAct()
                    + "','"
                    + borrowBean.getLsHasReturn()
                    + "')";
            int num = stmt.executeUpdate(sql);
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(rs, stmt, conn);
        }
        return false;
    }

    // 查询所有的User对象
    public ArrayList<BorrowBean> findAll() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<BorrowBean> list = new ArrayList<>();
        try {
            // 获得数据的连接
            conn = new JDBCUtil().GetConnection();
            // 获得Statement对象
            stmt = conn.createStatement();
            // 发送SQL语句
            String sql = "SELECT * FROM TB_Borrow";
            rs = stmt.executeQuery(sql);
            // 处理结果集
            while (rs.next()) {
                BorrowBean borrowBean = new BorrowBean();
                borrowBean.setRdID(rs.getString("rdID"));
                borrowBean.setBkID(rs.getString("bkID"));
                borrowBean.setLdDateOut(rs.getString("ldDateOut"));
                borrowBean.setLdDateRetPlan(rs.getString("ldDateRetPlan"));
                borrowBean.setLdDateRetAct(rs.getString("ldDateRetAct"));
                borrowBean.setLsHasReturn(rs.getString("lsHasReturn"));
                list.add(borrowBean);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(rs, stmt, conn);
        }
        return null;
    }

    // 根据id查找指定的user
    public ArrayList<BorrowBean> find(String id) {
        ArrayList<BorrowBean> borrowBeanList=new ArrayList<>();

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 获得数据的连接
            conn = new JDBCUtil().GetConnection();
            // 获得Statement对象
            stmt = conn.createStatement();
            // 发送SQL语句
            String sql = "SELECT * FROM TB_Borrow WHERE rdID=" + id;
            rs = stmt.executeQuery(sql);
            // 处理结果集
            while (rs.next()) {
                BorrowBean borrowBean = new BorrowBean();
                borrowBean.setRdID(rs.getString("rdID"));
                borrowBean.setBkID(rs.getString("bkID"));
                borrowBean.setLdDateOut(rs.getString("ldDateOut"));
                borrowBean.setLdDateRetPlan(rs.getString("ldDateRetPlan"));
                borrowBean.setLdDateRetAct(rs.getString("ldDateRetAct"));
                borrowBean.setLsHasReturn(rs.getString("lsHasReturn"));
                borrowBeanList.add(borrowBean);
            }
            return borrowBeanList;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(rs, stmt, conn);
        }
        return null;
    }

    // 删除用户
    public boolean delete(String id) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 获得数据的连接
            conn = new JDBCUtil().GetConnection();
            // 获得Statement对象
            stmt = conn.createStatement();
            // 发送SQL语句
            String sql = "DELETE FROM TB_Borrow WHERE rdID=" + id;
            int num = stmt.executeUpdate(sql);
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(rs, stmt, conn);
        }
        return false;
    }

    // 修改用户
    public boolean update(BorrowBean borrowBean) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 获得数据的连接
            conn = new JDBCUtil().GetConnection();
            // 获得Statement对象
            stmt = conn.createStatement();
            // 发送SQL语句

            String sql = "UPDATE TB_Borrow set " +
                    "rdID='" + borrowBean.getRdID() +
                    "',bkID='" + borrowBean.getBkID() +
                    "',ldDateOut='" + borrowBean.getLdDateOut() +
                    "',ldDateRetPlan='" + borrowBean.getLdDateRetPlan() +
                    "',ldDateRetAct='" + borrowBean.getLdDateRetAct() +
                    "',lsHasReturn='" + borrowBean.getLsHasReturn() +
                    "' WHERE rdID=" + borrowBean.getRdID();
            int num = stmt.executeUpdate(sql);
            if (num > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.release(rs, stmt, conn);
        }
        return false;
    }
}





